<?php
namespace app\weixin\controller;

use app\common\controller\LiteBase;
use app\common\util\Assert;
use app\common\util\DateTimeUtil;
use app\common\util\LoginManager;
use app\common\util\ValueBag;
use think\Input;

class QuestionAnswer extends LiteBase
{
    public function index()
    {
        LoginManager::doCheckLogin();
        return $this->fetch();
    }

    public function save()
    {
        if (IS_POST) {
            try {
                $db = \app\common\model\QuestionAnswer::instance();
                $postData = Input::post();
                $saveData = Assert::notEmpty($postData, ['username', 'telephone', 'question']);
                $saveData['question_time'] = DateTimeUtil::now();
                $saveData['answer'] = '';
                if (LoginManager::isLoginOn()) {
                    $saveData['member_id'] = LoginManager::getLoginUserId();
                } else {
                    $saveData['member_id'] = 0;
                }
                $lastId = $db->add($saveData);
                if ($lastId) {
                    return $this->jsonSuccess('提交成功');
                }
            } catch (\Exception $ex) {
                return $this->jsonFail($ex->getMessage());
            }
        }
        return $this->jsonFail('系统忙');
    }

    public function saveResult()
    {
        return $this->fetch();
    }

    public function detail()
    {
        $id = intval(Input::get('id'));
        if ($id) {
            $this->assign('m', ValueBag::wrap(\app\common\model\QuestionAnswer::instance()->find($id)));
            return $this->fetch();
        }
    }
}